import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author lzhphantom
 * @description
 * @date 创建于 16:44 2020/8/10
 */

public class LinkStack {
    private LinkNode root;
    private int size;


    public void push(Object val) {
        if (this.root == null) {
            this.root = new LinkNode();
            this.root.setVal(val);
        } else {
            LinkNode preNode = this.root;
            LinkNode newNode = new LinkNode();
            newNode.setVal(val);
            newNode.setNext(preNode);
            this.root = newNode;
        }
        this.size++;
    }

    public Object pop() throws Exception {
        if (this.size == 0) {
            throw new Exception("this LinkStack is empty");
        }
        LinkNode topNode = this.root;
        Object result = topNode.getVal();
        this.root = topNode.getNext();
        this.size--;

        return result;
    }

    public boolean isEmpty() {
        return this.size == 0;
    }
}

@Data
class LinkNode {
    private LinkNode next;
    private Object val;
}
